#include <stdio.h>
#define NS 100 // тесты
#define MS 100 // попытки

int
main ()
{
	int n, table[NS], bonus, m;
	short history[MS][NS], combo=0;
	int results[MS];
	int tmp=0;
	register short i=0, j=0;

	freopen("results.in", "r", stdin);
	freopen("results.out", "w", stdout);

	scanf("%d", &n);
	while (i < n)
		scanf("%d", &table[i++]);
	scanf("%d\n%d", &bonus, &m);
	i = 0;
	while (j < m) {
		while (i < n) {
			scanf("%hd", &history[j][i++]);
		}
		++j, i = 0;
	}

	i = j = 0;

	while (j < m) {
		combo = 0;
		tmp = 0;
		i = 0;
		while (i < n) {
			if (history[j][i]) {
				tmp += table[i];
				++combo;
			}
			++i;
		}
		if (combo == n)
			tmp += bonus;

		results[j] = tmp - j * 2;

		if (results[j] < 0)
			results[j] = 0;

		if (j > 0 && results[j-1] > results[j])
			results[j] = results[j-1];
		++j;
	}

	j = 0;
	while (j < m)
		printf("%d\n", results[j++]);
	return 0;
}
